package com.Crawlzongheng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.CrawlData.CrawlBase;
import com.Model.NovelChapterModel;
import com.Util.ParseMD5;
import com.Util.RegexUtil;
import com.Util.jsonUtil;
import com.zonghengdb.ZongHengDB;
/**
 * 章节列表页信息采集
 * @author 89596
 *
 */
public class ChapterPage extends CrawlBase{
	private String url;
	private static HashMap<String, String> params;
	//章节也信息正则表达式
	private static final String CHAPTER = "<td class=\"chapterBean\" chapterId=\"\\d*?\" chapterName=\"(.*?)\" chapterLevel=\"\\d*?\" wordNum=\"(\\d*?)\" updateTime=\"(\\d*?)\"><a href=\"(.*?)\" title=\".*?\">";
	//正则表达式中所需要查询的字段
	private static final int[] array = {1,2,3,4};
	
	static {
		params = new HashMap<String, String>();
		params.put("Referer", "http://book.zongheng.com");
		params.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
		params.put("Host", "book.zongheng.com");
	}
	/**
	 * 获取小说章节信息的list
	 * @return
	 */
	public List<NovelChapterModel> analyzer(){
		List<NovelChapterModel> list = new ArrayList<NovelChapterModel>();
		List<String[]> arrays = getChapters();
		int i = 0;
		for(String[] array:arrays){
			i++;
			list.add(analyzer(array, i));
			
		}
		return list;
		
	}
	
	private NovelChapterModel analyzer(String[] array,int i){
		NovelChapterModel bean = new NovelChapterModel();
		bean.setUrl(array[3]);
		bean.setId(ParseMD5.parseStrToMD5(bean.getUrl()));
		bean.setTitle(array[0]);
		bean.setWordCount(Integer.parseInt(array[1]));
		bean.setChapterTime(Long.parseLong(array[2]));
		
		bean.setChapterId(i);
		return bean;
	}
	
	public ChapterPage(String url) {
		readPageByGet(url, params, "utf-8");
		this.url=url;
	}
	/**
	 * 获得章节的信息
	 * @return 章节信息的list
	 */
	public List<String[]> getChapters(){
		return RegexUtil.getListString(getPageSourceCode(), CHAPTER, array);
	}
	
	
	
	public static void main(String[] args) {
		ChapterPage chapterPage = new ChapterPage(
				"http://book.zongheng.com/showchapter/497428.html");
		for (String[] ss : chapterPage.getChapters()) {
			for (String s : ss) {
				System.out.println(s);
			}
			System.out.println("----------------");
		}
		System.out.println(jsonUtil.parseJson(chapterPage.analyzer()));
		//06b331433e44f1c1a9a0f304d7e24201
		ZongHengDB db = new ZongHengDB();
		db.saveNovelChapter(chapterPage.analyzer(), ParseMD5.parseStrToMD5("http://book.zongheng.com/book/497428.html"));
		
	}
	
}
